package com.twu.biblioteca;

import java.util.ArrayList;

/**
 * Created by jrocha on 9/22/14.
 */

public class Library {

    public Library(){
        this(new User("", "", "", "", ""));
    }

    private void initializeBooks() {
        this.booksAvailable = new ArrayList<Book>();
        this.booksAvailable.add(new Book(234, "TDD: By Example", "Kent Beck", 2002));
        this.booksAvailable.add(new Book(345, "The Pragmatic Programmer", "Andrew Hunt", 1999));
        this.booksAvailable.add(new Book(456, "Design Patterns", "Erich Gamma", 1994));
        this.booksAvailable.add(new Book(567, "Refactoring", " Martin Fowler", 1999));
    }

    public Library(User autheticatedUser) {
        this.autheticatedUser = autheticatedUser;
        initializeBooks();

        this.moviesAvailable = new ArrayList<Movie>();
        this.moviesAvailable.add(new Movie(122, "Prayers for Bobby", 2009, "Russell Mulcahy", 8));
        this.moviesAvailable.add(new Movie(432, "Praia do Futuro", 2014, "Karim Ainouz", 7));
        this.moviesAvailable.add(new Movie(240, "Hoje eu quero voltar sozinho", 2014, "Daniel Ribeiro", 8));
    }

    public ArrayList<ItemLibrary> listBooksAvailable() {
        ArrayList<ItemLibrary> books = new ArrayList<ItemLibrary>();
        for (int b = 0; b < booksAvailable.size(); ++b){
            if (!booksAvailable.get(b).isCheckedOut()){
                books.add(booksAvailable.get(b));
            }
        }
        return books;
    }

    public ArrayList<Book> listBooksCheckedOut() {
        ArrayList<Book> books = new ArrayList<Book>();
        for (int b = 0; b < booksAvailable.size(); ++b){
            if (booksAvailable.get(b).isCheckedOut()){
                books.add(booksAvailable.get(b));
            }
        }
        return books;
    }

    public int listContainsBook(int bookIdInt) {
        int bookPosition = 0;
        boolean existBook = false;

        while (bookPosition < booksAvailable.size() && !existBook){
            int id = booksAvailable.get(bookPosition).getId();
            if(id == bookIdInt){
                existBook = true;
            }
            else
                ++bookPosition;
        }
        return bookPosition;
    }

    public String checkOutBook(int bookId) {
        int bookPosition = listContainsBook(bookId);

        if (bookPosition < booksAvailable.size() && !booksAvailable.get(bookPosition).isCheckedOut()) {
            Book book = booksAvailable.get(bookPosition);
            book.setCheckedOut(true);
            book.setUser(autheticatedUser);

            message = "Thank you! Enjoy the book";
        }
        else {
            message = "That book is not available";
        }

        return message;
    }

    public String returnBook(int bookId) {
        int bookPosition = listContainsBook(bookId);

        if (bookPosition < booksAvailable.size() && booksAvailable.get(bookPosition).isCheckedOut()) {
            booksAvailable.get(bookPosition).setCheckedOut(false);

            message = "Thank you for returning the book.";
        }
        else {
            message = "That is not a valid book to return";
        }

        return message;
    }

    public int listContainsMovie(int movieIdInt) {
        int moviePosition = 0;
        boolean existMovie = false;

        while (moviePosition < moviesAvailable.size() && !existMovie){
            int id = moviesAvailable.get(moviePosition).getId();
            if(id == movieIdInt){
                existMovie = true;
            }
            else
                ++moviePosition;
        }
        return moviePosition;
    }


    public ArrayList<ItemLibrary> listMoviesAvailable() {
        ArrayList<ItemLibrary> movies = new ArrayList<ItemLibrary>();
        for (int m = 0; m < moviesAvailable.size(); ++m){
            if (!moviesAvailable.get(m).isCheckedOut()){
                movies.add(moviesAvailable.get(m));
            }
        }
        return movies;
    }

    public String checkOutMovie(int movieId) {
        int moviePosition = listContainsMovie(movieId);

        if (moviePosition < moviesAvailable.size() && !moviesAvailable.get(moviePosition).isCheckedOut()) {
            moviesAvailable.get(moviePosition).setCheckedOut(true);

            message = "Thank you! Enjoy the movie";
        }
        else {
            message = "That movie is not available";
        }

        return message;
    }

    public String showUserInformation(){
        return autheticatedUser.toString();
    }

    public String returnMovie(int movieId) {
        int moviePosition = listContainsMovie(movieId);

        if (moviePosition < moviesAvailable.size() && moviesAvailable.get(moviePosition).isCheckedOut()) {
            moviesAvailable.get(moviePosition).setCheckedOut(false);

            message = "Thank you for returning the movie.";
        }
        else {
            message = "That is not a valid movie to return";
        }

        return message;
    }

    private String message = "";

    private ArrayList<Book> booksAvailable;
    private ArrayList<Movie> moviesAvailable;

    private User autheticatedUser;

}
